﻿#region Using

using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Drawing;
using System.Media;

#endregion

namespace ExtendedDialogBox
{
	public static class ExtendedMessageBox
	{
		#region Variables
		
		public static FlatStyle buttonStyle = FlatStyle.Standard;
		public static string backgroundImagePath = "";
		public static Color backgroundColor = Color.Empty;
		public static Font font = SystemFonts.DefaultFont;
		
		public static string OkButtonText = "Ok";
		public static string YesButtonText = "Yes";
		public static string NoButtonText = "No";
		public static string CancelButtonText = "Cancel";
		
		#endregion
		
		#region Methods
		
		/// <summary>
		/// Mostra la finestra Modale Windows Form
		/// </summary>
		/// <param name="message">Corpo del messaggio</param>
		/// <param name="title">Titolo del messaggio</param>
		/// <param name="textColor">Colore del testo</param>
		/// <param name="buttons">Imposta i tipi di bottoni</param>
		/// <param name="icon">Icona del messaggio</param>
		/// <returns>Ritorna il dialog result</returns>
		public static DialogResult Show(string message, string title, Nullable<Color> textColor, ExtendedDialogBoxButtons flagsButton, MessageBoxIcon icon)
		{
			DialogResult result = DialogResult.None;
			using (Form frm = new Form())
			{
				frm.TopMost = true;
				frm.StartPosition = FormStartPosition.CenterScreen;
				frm.MinimizeBox = false;
				frm.MaximizeBox = false;
				frm.FormBorderStyle = FormBorderStyle.FixedDialog;
				frm.ClientSize = new Size(400, 200);
				frm.Font = font;
				frm.Text = title;
				frm.Icon = function.GetIcon(icon);
				
				if (backgroundColor != Color.Empty)
					frm.BackColor = backgroundColor;
				else
				{
					if (!string.IsNullOrEmpty(backgroundImagePath))
					{
						frm.BackgroundImage = Image.FromFile(backgroundImagePath);
						frm.BackgroundImageLayout = ImageLayout.Stretch;
					}
				}
				
				List<Button> buttons = new List<Button>();
				
				if (flagsButton.HasFlag(ExtendedDialogBoxButtons.Ok))
					buttons.Add(function.GetButtonDetails(OkButtonText, buttonStyle, DialogResult.OK));
				if (flagsButton.HasFlag(ExtendedDialogBoxButtons.Yes))
					buttons.Add(function.GetButtonDetails(YesButtonText, buttonStyle, DialogResult.Yes));
				if (flagsButton.HasFlag(ExtendedDialogBoxButtons.No))
					buttons.Add(function.GetButtonDetails(NoButtonText, buttonStyle, DialogResult.No));
				if (flagsButton.HasFlag(ExtendedDialogBoxButtons.Cancel))
					buttons.Add(function.GetButtonDetails(CancelButtonText, buttonStyle, DialogResult.Cancel));
				
				function.SetButtonsLocation(ref buttons, frm.ClientSize);
				
				int count_buttons = buttons.Count;
				
				for (int i = 0; i < count_buttons; i++)
					buttons[i].Click += (o, e) => { frm.Close(); };
				
				for (int i = 0; i < count_buttons; i++)
					frm.Controls.Add(buttons[i]);
				
				Label label = function.GetLabel(message, textColor, frm.ClientSize, font);
				
				frm.ClientSize = new Size(frm.ClientSize.Width, label.Height + buttons[0].Height + 30);
				
				frm.Controls.Add(label);
							
				function.PlaySound(icon);
				
				result = frm.ShowDialog();
			}
			return result;
		}
		
		#endregion
	}
	
}